import * as chartService from 'services/chart'
import DataSet from '@antv/data-set'

function getLocationStr (val) {
  if (val) {
    let str = ""
    const {province, city, area} = val
    const arr = []
    if (province) arr.push(province)
    if (city) arr.push(city)
    if (area) arr.push(area)

    arr.forEach((txt, idx) => {
      str = str.concat(txt)
      if (idx < arr.length - 1) {
        str = str.concat(", ")
      }
    })
    return str
  }
}

export default {
  namespace: 'chart',

  state: {
    saleAmountStat: {},
    orderStat: {},
    visitData: {},
    saleProgress: {},
    operatorStat: [],
    incomingLine: [],
    saleData: [],
    incomeData: [],
    saleRankings: [],
    data: [],
    geoData: {},
  },

  subscriptions: {
    setup ({dispatch, history}) {
      history.listen((location) => {
        if (location.pathname === '/dashboard') {
          dispatch({
            type: 'fetch',
          })
          dispatch({type: 'chinaGeo'})
        }
      })
    },
  },

  effects: {

    * chinaGeo ({payload = {}}, {call, put}) {
      const response = yield call(chartService.chinaGeo)
      const mapResp = yield call(chartService.mapdata, payload)

      const dv = new DataSet.View().source(response, {
        type: 'GeoJSON',
      }).transform({
        type: 'geo.projection',
        projection: 'geoMercator',
        as: ['x', 'y', 'centroidX', 'centroidY'],
      })

      const userData = new DataSet.View().source(mapResp.data).transform({
        type: 'map',
        callback: (obj) => {
          const projectedCoord = dv.geoProjectPosition([obj.lng, obj.lat], 'geoMercator')
          obj.x = projectedCoord[0]
          obj.y = projectedCoord[1]
          const str = obj['成交额']
          obj.deaths = str.substr(2, str.length) * 1
          return obj
        },
      })

      yield put({
        type: 'save',
        payload: {
          data: userData,
          geoData: dv,
        },
      })
    },

    * fetch (_, {call, put}) {
      let platformSaleStatData = yield call(chartService.platformSaleStatData)
      if (platformSaleStatData && platformSaleStatData.code === 0) {
        platformSaleStatData = platformSaleStatData.data
      }

      let platformStatData = yield call(chartService.platformStatData)
      if (platformStatData && platformStatData.code === 0) {
        platformStatData = platformStatData.data
        if (platformStatData && platformStatData.operatorStat) {
          platformStatData.operatorStat = platformStatData.operatorStat.map(val => {
            return {x: val.x, y: parseInt(val.y)}
          })
        }

        if (platformStatData && platformStatData.incomingLine) {
          platformStatData.incomingLine = platformStatData.incomingLine.map(val => {
            return {
              time: val.dateTime,
              '进线数': val.visitCount,
              '沟通数': val.contactNum,
              rate: val.communicationRate,
            }
          })
        }
      }

      let saleLineData = yield call(chartService.saleLineData)
      if (saleLineData && saleLineData.code === 0) {
        saleLineData = saleLineData.data

        saleLineData.incomeData.map(e => e['y'] = parseInt(e['y']))
        saleLineData.saleData.map(e => e['y'] = parseInt(e['y']))
      }

      let saleRankings = yield call(chartService.getSaleRankings)
      if (saleRankings && saleRankings.code === 0) {
        saleRankings = saleRankings.data
      }

      const payload = {
        ...platformSaleStatData,
        ...platformStatData,
        ...saleLineData,
        saleRankings,
      }

      yield put({
        type: 'save',
        payload,
      })
    },

    * fetchSalesData ({payload = {}}, {call, put}) {
      let response = yield call(chartService.getSaleRankings, payload)
      if (response && response.code === 0) {
        response = response.data
      }
      yield put({
        type: 'save',
        payload: {
          saleRankings: response,
        },
      })
    },
  },

  reducers: {
    save (state, {payload}) {
      return {
        ...state,
        ...payload,
      }
    },
    clear () {
      return {
        saleAmountStat: {},
        orderStat: {},
        visitData: {},
        saleProgress: {},
        operatorStat: [],
        incomingLine: [],
        saleData: [],
        incomeData: [],
        saleRankings: [],
      }
    },
  },
}
